<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 09/08/2017
 * Time: 19:38
 */

namespace common\models;

/**
 * 权限管理
 * Class Permission
 *
 * @todo 后期实现基于RBAC方式细分权限控制
 */
class Permission
{
    private $uid;//主账户ID
    private $uidManage;//管理账户ID
    private $uidSubject;//待查询用户ID
    private $excludeFirstLevelCheck = false;//平台级是否不需要验证，直接通过


    public function __construct($uid, $uidManage, $uidSubject, $excludeFirstLevelCheck = false)
    {
        $this->uid = $uid;
        $this->uidManage = $uidManage;
        $this->uidSubject = $uidSubject;
        $this->excludeFirstLevelCheck = $excludeFirstLevelCheck;
    }

    public function check()
    {
        //平台级直接跳过
        if ($this->excludeFirstLevelCheck) {
            $obj = UserDao::getUserObj($this->uid);
            if (!$obj) {
                return false;
            }

            $levelInfo = Common::getLevelsInfo($obj->level);
            if ($levelInfo['is_first'] == 1) {
                return true;
            }
        }

        //直属上级才有权限
        $obj = UserDao::getUserObj($this->uidSubject);
        if (!$obj || $obj->pid != $this->uid) {
            return false;
        }

        return true;
    }
}